<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/21
 * Time: 13:32
 */

namespace app\admin\controller;
use app\admin\model\Base;
use think\Request;
use think\Db;
class Ategory  extends Common
{
    /**
     * @var string
     * 初始化数据,创建对象，定义表,控制器
     */
      public $table='ategory';
      public $obj='';
      public function _initialize(){
          $this->check();
          parent::_initialize();

      }

    /**
     * @return string
     * 判断对象事是否存在
     */
       public function check(){
           if($this->obj==""){
               $this->obj=new Base();
           }else{
               return $this->obj;
           }
       }

    /**
     * @return mixed
     *  首页展示
     */
       public function show(){
           $result=$this->obj->selOne($this->table,1);
           foreach ($result as $kk=>$val){
               foreach ($val as $key=>$value){
                   if($key=='deleted' && $value=='0'){
                       $result[$kk][$key]="不存在";
                   }elseif($key=='deleted' && $value=='1'){
                       $result[$kk][$key]="存在";
                   }
               }

           }
           if($result){
               $this->assign("result",$result);
               return  $this->fetch($this->table.'/show');
           }else{
               $this->error('查询数据失败');
           }
       }

    /**
     * 添加数据
     */
      public function add(){
          $date=Request::instance()->post();
          $id=array_shift($date);
          if($date['deleted']=='添加'){
              $date['deleted']=1;
          }else{
              $date['deleted']=0;
          }
          $res=$this->obj->add($this->table,$date);
          if($res){
              $this->success("添加成功");
          }else{
              $this->error("添加失败");
          }
      }

    /**
     * 修改数据
     */
       public function update(){
               $date=Request::instance()->post();
               $id=array_shift($date);
               if($date['deleted']=='添加'){
                   $date['deleted']=1;
               }else{
                   $date['deleted']=0;
               }
             if($this->obj->upd($this->table,$id,$date)){
                $this->success("修改成功");
             }else{
                 $this->error("修改数据失败");
             }

       }
       /**
        * 假删除数据
        */
       public function del($id){
           $date=['deleted'=>'0'];
           if($this->obj->upd($this->table,$id,$date)){
               echo 1;
           }else{
               echo 0;
           }

       }
       /**
        * 恢复数据
        */
       public function restore(){
           $result=$this->obj->sel($this->table);
           foreach ($result as $kk=>$val){
               foreach ($val as $key=>$value){
                   if($key=='deleted' && $value=='0'){
                       $date=['deleted'=>1];
                       $this->obj->upd($this->table,$result[$kk]['id'],$date);
                   }
               }
           }
          $this->success("恢复成功");
       }

    /**
     * @param $mm
     * @return string
     * 收索框
     */
    public function checks($mm){
          $result=$this->obj->selField($this->table,'goods_class_name',$mm);
          return json_encode($result);
    }
}